package com.qipay.core.help;

import com.qipay.baas.model.Dictionary;
import com.qipay.baas.model.DictionaryExample;
import com.qipay.core.mapper.DictionaryMapper;
import com.qipay.core.utils.CommUtil;
import com.qipay.core.utils.SpringContextUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典表帮助类
 */
public class DicHelper {
    public static final String USER_TYPE = "userType";

    /*******************以上用于写静态代码值**************************/

    /**
     * 用于缓存
     */
    private static List<Dictionary> dictionaries;

    /**
     * 缓存开关,缓存开的时候,第一次会去库里取字典,缓存关,每次都查表
     */
    private static final boolean CACHE_FLAG = false;

    /**
     * 根据代码类型获取代码列表
     *
     * @param dictType
     * @return
     */
    public static List<Dictionary> getCodeList(String dictType) {
        if (CommUtil.isEmpty(dictType)) return new ArrayList<Dictionary>();
        List<Dictionary> collect = allDic()
                .stream()
                .filter(dic -> CommUtil.equalsString(dic.getDictType(), dictType))
                .collect(Collectors.toList());
        return collect;
    }

    /**
     * 取得所有字典
     *
     * @return
     */
    private static List<Dictionary> allDic() {
        //无字典或有值关缓存,取字典
        if (dictionaries == null || (dictionaries != null && !CACHE_FLAG)) {
            DictionaryExample dictionaryExample = new DictionaryExample();
            DictionaryMapper dictionaryMapper = (DictionaryMapper) SpringContextUtil.getBean(DictionaryMapper.class);
            dictionaries = dictionaryMapper.selectByExample(dictionaryExample);
        }
        return dictionaries;
    }

    /**
     * 根据代码类型和代码名称获取代码
     *
     * @param dictType
     * @param dictName
     * @return
     */
    public static Dictionary getCodeByName(String dictType, String dictName) {
        if (CommUtil.isEmpty(dictType)) return null;
        Dictionary dictionary = getCodeList(dictType)
                .stream()
                .filter(dic -> CommUtil.equalsString(dic.getDictName(), dictName))
                .findFirst().get();
        return dictionary;
    }


    /**
     * 根据代码类型和代码获取代码对象
     *
     * @param dictType
     * @param dictCode
     * @return
     */
    public static Dictionary getCodeByCode(String dictType, Integer dictCode) {
        Dictionary dictionary = getCodeList(dictType)
                .stream()
                .filter(dic -> CommUtil.equalsNumber(dic.getDictValue(), dictCode))
                .findFirst().get();
        return dictionary;
    }

    /**
     * 根据代码类型和代码获取代码的父亲的码值!
     *
     * @param dictType 字典类型
     * @param dictCode 要查的码值
     * @return
     */
    public static Integer getParentCode(String dictType, Integer dictCode) {
        Dictionary dictionary = getCodeByCode(dictType, dictCode);
        if(CommUtil.isNotEmpty(dictionary)){
            return dictionary.getParentCode();
        }
        return null;
    }

    /**
     * 根据代码类型和代码名称获取代码值
     *
     * @param dictType
     * @param dictName
     * @return
     */
    public static Integer getCodeValue(String dictType, String dictName) {
        Dictionary dictionary = getCodeList(dictType)
                .stream()
                .filter(dic -> CommUtil.equalsString(dic.getDictName(), dictName))
                .findFirst().get();

        if(CommUtil.isNotEmpty(dictionary)){
            return dictionary.getDictValue();
        }
        return null;
    }

    /**
     * 根据代码和代码值获取代码中文名称
     *
     * @param dictType
     * @param dictCode
     * @return
     */
    public static String getDictCnName(String dictType, Integer dictCode) {
        Dictionary dictionary = getCodeByCode(dictType,dictCode);

        if(CommUtil.isNotEmpty(dictionary)){
            return dictionary.getDictName();
        }
        return null;
    }

}
